const LogModel = require('../../model/Log');
const moment = require('moment')
const Staff = require('../../model/Staff')
const User = require('../../model/User')

module.exports = {

    changeStaff: async(req, res, next) => {
        let user = await User.findOne({ cellphone: req.body.cellphone, isDelete: 0 }).exec()
        if (user != null) {
            openid = user.openid,
                unionID = user.unionID
        } else {
            openid = undefined,
                unionID = undefined
        }
        if (req.body.id) {
            let othersStaff = await Staff.findOne({_id:{$ne:req.body.id},cellphone:req.body.cellphone,isDelete:0}).exec()
            if(othersStaff!==null){
                return res.send({code:201,msg:"手机号已被使用"})
            }
            let staff = await Staff.findOne({ _id: req.body.id, isDelete: 0 }).exec()
            staff.openid = openid,
                staff.unionID = unionID,
                staff.cellphone = req.body.cellphone,
                staff.updatedAt = new Date(),
                staff.name = req.body.name,
                staff.store = req.body.store,
                await staff.save()
            res.ok('修改成功')
        } else {
            let othersStaff = await Staff.findOne({cellphone:req.body.cellphone,isDelete:0}).exec()
            if(othersStaff!==null){
                return res.send({code:201,msg:"手机号已被使用"})
            }
            let newStaff = new Staff({
                openid: openid,
                unionID: unionID,
                cellphone: req.body.cellphone,
                updatedAt: new Date(),
                name: req.body.name,
                store: req.body.store,
                isDelete: 0,
            })
            await newStaff.save()
            res.ok('创建成功')
        }
    },

    getOneStaff: async(req, res, next) => {
        try {
            let staff = await Staff.findOne({ _id: req.body.id, isDelete: 0 }).populate('store').exec()
            return res.ok('查询员工成功', staff)
        } catch (e) {
            next(e)
        }
    },


    getStaffList: async(req, res, next) => {
        try {
            let option = {}
            option.isDelete = 0
            if (req.body.title) {
                option.name = new RegExp(req.body.name, 'i')
            }
            if (req.body.cellphone) {
                option.cellphone = new RegExp(req.body.cellphone)
            }
            let count = await Staff.countDocuments(option).exec()
            let page = req.body.page || 1
            let pageSize = req.body.pageSize || count
            let data = await Staff.find(option).skip((page - 1) * pageSize).limit(pageSize).populate('store').exec()
            let result = {}
            result.total = count
            result.currentPage = page
            result.data = data
            result.pageSize = pageSize
            return res.ok('获取列表成功', result)
        } catch (e) {
            next(e)
        }
    },

    deleteStaff: async(req, res, next) => {
        try {
            let staff = await Staff.findOne({ _id: req.body.id }).exec()
            staff.isDelete = 1
            await staff.save()
            return res.ok('删除员工成功')
        } catch (e) {
            next(e)
        }
    },

};